package com.note7_5;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

public class Question7 {

	/*
	 * 常用图片的格式可以通过读取文件头部分获得：常用图片的格式可以通过读取文件头部分获得(16进制)：
	*	（1）JPEG
	*		- 文件头标识 (2 bytes): 0xff, 0xd8 (SOI) (JPEG 文件标识)
	*	（3）PNG
	*		- 文件头标识 (8 bytes)   89 50 4E 47 0D 0A 1A 0A
	*	（4）GIF
	*		- 文件头标识 (6 bytes)   47 49 46 38 39(37) 61 
	*	（5）BMP
	*		- 文件头标识 (2 bytes)   42 4D，字符即：        B  M
	*	输入一个文件名，判断其真正格式和其文件类型是否匹配（防止直接给文件改名，比如把一个jpg改成bmp或者把一个rar改成jpg）
	 */
	/***
	 * 
	 * @author 60973
	 * 首先需要获取文件名称，判断文件名后缀是否为题中几种
	 * 其次获得文件头标识
	 * 判断文件头标识与题目中文件头标识是否相同
	 * 
	 */
	
	private static String[] fileName = {"JPEG","jpeg","PNG","png","GIF","gif","BMP","bmp"};
	private static String [] fileHeadName = {"FFD8","89504E470D0A1A0A","474946383961","474946383761","424D"};
	
	//判断文件名是否属于该程序中的图片格式
	public static boolean isFileName(String fileLastName){
		for(String s:fileName){
			if(fileLastName.equals(s)){
				return true;
			}
		}
		return false;
	}
	
	//将bye数组转化成16进制的字符串
	public static String bytesToHexString(byte[] bs) {  
        String s = new String();  
        if (bs == null || bs.length <= 0) {  
            return null;  
        }  
        String Sixteen;  
        for (byte b:bs) {  
            //toHexString将()中的int型转化为16进制		toUpperCase()  将字符转化为大写  
        	Sixteen = Integer.toHexString(b & 0xFF).toUpperCase();
        	//10-15都为单个字母，前面加0，保持两位
            if (Sixteen.length() < 2) {  
                Sixteen = "0" +  Sixteen; 
            }  
            s = s + Sixteen;  
        }  
        return s;  
    }
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入要判断的文件：");
		String path =in.nextLine(); 
		//"C:\\Users\\60973\\Desktop\\小图标\\android\\图片素材\\小图标\\0.png"
		File f = new File(path);
		String[] fileLastName = f.getName().split("\\.");
		String lastName = f.getName().substring(1+f.getName().lastIndexOf("."));
		if(isFileName(fileLastName[fileLastName.length-1])){
			InputStream is = null;
			try {
				int i = 8;	//byte数组的大小
				int j = 0;	//fileHeadName数组的位置
				if(fileLastName[fileLastName.length-1].equals("png")){
					j = 1;
				}
				if(fileLastName[fileLastName.length-1].equals("jpeg")){
					i = 2;
					j = 0;
				}
				if(fileLastName[fileLastName.length-1].equals("gif")){
				//D:\\驱动精灵\\DriverGenius\\data\\nicdrvhelp\\images\\image022.gif
					i = 6;
					j = 2;
					is = new FileInputStream(path);
					byte[] bs = new byte[i];
					int a1 = is.read(bs);
					String s = bytesToHexString(bs);
					System.out.println(s);
					System.out.println(fileHeadName[j]);
					if(s.equals(fileHeadName[2])||s.equals(fileHeadName[3])){
						System.out.println("文件后缀与文件头标识匹配！");
					}else{
						System.out.println("文件后缀与文件头标识不匹配！");
					}
					return;
				}
				if(fileLastName[fileLastName.length-1].equals("bmp")){
					i = 2;
					j = 4;
				}
				is = new FileInputStream(path);
				byte[] bs = new byte[i];
				int a1 = is.read(bs);
				String s = bytesToHexString(bs);
				System.out.println(s);
				System.out.println(fileHeadName[j]);
				if(s.equals(fileHeadName[j])){
					System.out.println("文件后缀与文件头标识匹配！");
				}else{
					System.out.println("文件后缀与文件头标识不匹配！");
				}
			} catch (FileNotFoundException e) {
				System.out.println("没有找到文件");
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					is.close();
				} catch (IOException e) {
					System.out.println("输入流关闭失败");
					e.printStackTrace();
				}
			}
		}else{
			System.out.println(fileLastName[fileLastName.length-1]);
			System.out.println("该文件不属于该程序可识别图片类型！");
		}
	}
}
